package com.wgj.android.ui.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiaai.xui.utils.imageload.XImage;
import com.wgj.android.R;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PictureActivity extends AppCompatActivity {

    @BindView(R.id.viewpage)
    ViewPager viewpage;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.tv_num)
    TextView tvNum;
    private String imgs;
    private String img[];
    private String title[];
    private List<ImageView> imageViews = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private Timer timer;
    private TimerTask task;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);
        ButterKnife.bind(this);
        initView();
        timer();

    }

    private void timer() {
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                try {

                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        };
        timer.schedule(task,10000);
    }

    private void initView() {
        imgs = getIntent().getStringExtra("imgs");
        try {
            //imgs.replace("""","");
            JSONArray array = new JSONArray(imgs);
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.getJSONObject(i);
                titles.add(object.getString("title"));
                ImageView image = new ImageView(this);
                XImage.getInstance().load(image, object.getString("img"));
                imageViews.add(image);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        textView.setText(titles.get(0));
        tvNum.setText("1/"+imageViews.size());
        viewpage.setAdapter(new MyPagerAdapter());

        viewpage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页面改变的时候调用(稳定),positon表示被选中的索引
            @Override
            public void onPageSelected(int position) {
                textView.setText(titles.get(position));//设置文字描述内容
                tvNum.setText(position+"/"+imageViews.size());
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
            }

            @Override
            //页面滚动状态发送改变的时候回调
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {

            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imageViews.get(position));
            return imageViews.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timer!=null){
            timer.cancel();
        }

    }
}
